package com.sce.core.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.sce.core.mongodb.props.MultipleMongoProperties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.stereotype.Service;

import javax.net.ssl.SSLSocketFactory;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hylc on 2018/6/18.
 */
@Service
@Configuration
public class MultipleMongoConfig {

    @Autowired
    private MultipleMongoProperties mongoProperties;

    @Primary
    @Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate primaryMongoTemplate( ) throws Exception {
        return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
    }

    @Bean
    @Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate secondaryMongoTemplate() throws Exception {
        return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
    }



    @Bean
    @Primary
    public MongoDbFactory primaryFactory(MongoProperties primary) {

        ServerAddress serverAddress = new ServerAddress(primary.getHost(), primary.getPort());
        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        mongoCredentialList.add(MongoCredential.createCredential(primary.getUsername(), primary.getDatabase(), primary.getPassword()));
		return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), primary.getDatabase());
       // mongoCredentialList.add(MongoCredential.createCredential(primary.getUsername(), primary.getDatabase(), primary.getPassword()));
        //MongoClientOptions o = new MongoClientOptions.Builder().build();
        //return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList),primary.getDatabase());
    }


    @Bean
    public MongoDbFactory secondaryFactory(MongoProperties secondary) {

        ServerAddress serverAddress = new ServerAddress(secondary.getHost(), secondary.getPort());
        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        mongoCredentialList.add(MongoCredential.createCredential(secondary.getUsername(), secondary.getDatabase(), secondary.getPassword()));
		return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), secondary.getDatabase());
       // mongoCredentialList.add(MongoCredential.createCredential(secondary.getUsername(), secondary.getDatabase(), secondary.getPassword()));
        //MongoClientOptions o = new MongoClientOptions.Builder().build();
       // return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList),secondary.getDatabase());
    }
}
